package com.yunshang.shoppingmall.controller.product;

import com.yunshang.shoppingmall.common.UploadImgs;
import com.yunshang.shoppingmall.entity.ProductGuiGe;
import com.yunshang.shoppingmall.entity.ProductPrice;
import com.yunshang.shoppingmall.service.ProductGuiGeService;
import com.yunshang.shoppingmall.service.ProductPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AddGuiGeController {


    @Autowired
    ProductGuiGeService productGuiGeService;
    @Autowired
    ProductPriceService productPriceService;

    private int productIds = 0;
    private String guigeImgPath = "";
    private String guigeGroupImgs = "";
    String[] guigeGroupArr = new String[10];
    List<String> groupList = new ArrayList();

    //查看规格页面----------增加商品规格
    @ResponseBody
    @RequestMapping("/openAddGuige")
    public ModelAndView openAddGuige(@RequestParam("productId") String productId) {
        System.out.println("----->add");
        ModelAndView modelAndView = new ModelAndView("product/addGuige");
        if (productId != null && productId != "") {
            productIds = Integer.parseInt(productId);
        }
        modelAndView.addObject("productId", productId);
        return modelAndView;
    }


    //编辑规格----上传规格图
    @ResponseBody
    @RequestMapping(value = "/addGuiGeImgs", method = RequestMethod.POST)
    public Map<String, Object> addGuiGeImgs(MultipartFile file, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map = UploadImgs.uploads(file);
        guigeImgPath = (String) map.get("imagePath");
        System.out.println("imagePath:" + guigeImgPath);
        if (guigeImgPath != null && guigeImgPath != "") {
            map.put("msg", "1");
            map.put("guigeImgPath", guigeImgPath);
        }
        return map;
    }

    //编辑规格----上传图组
    @ResponseBody
    @RequestMapping(value = "/addGuiGeGroupImgs", method = RequestMethod.POST)
    public Map<String, Object> addGuiGeGroupImgs(MultipartFile file, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map = UploadImgs.uploads(file);
        guigeGroupImgs = (String) map.get("imagePath");
        groupList.add(guigeGroupImgs);
        if (guigeGroupImgs != null && guigeGroupImgs != "") {
            map.put("msg", "1");
        }
        return map;
    }


    //保存增加规格和价格数据
    @ResponseBody
    @RequestMapping(value = "/addGuigeAndPriceByProductId", method = RequestMethod.POST)
    public Map<String, Object> addGuigeAndPriceByProductId(@RequestParam Map<String, String> guigeMap) {
        Map<String, Object> map = new HashMap<>();
        double proHeight = 0.0;
        double proLength = 0.0;
        double proSize = 0.0;
        double proWeight = 0.0;
        double proTiji = 0.0;
        double proWidth = 0.0;
        double proVolume = 0.0;
        int productId = 0;
        BigDecimal proPrice = null;
        BigDecimal proMarketPrice = null;
        BigDecimal proCost = null;
        BigDecimal opratorCost = null;
        BigDecimal coupon = null;
        int visitor = 0;
        int huiYuan = 0;
        int dianZhu = 0;

        int YZDianZhu = 0;
        int otherRank = 0;
        int buyCount = 0;
        int buyCountTotal = 0;
        if (guigeMap != null) {
            //保存规格

            ProductGuiGe productGuiGe = new ProductGuiGe();
            productGuiGe.setGuigeName(guigeMap.get("guieName"));
            if (guigeMap.get("productId") != null && guigeMap.get("productId") != "") {
                productId = Integer.parseInt(guigeMap.get("productId"));
            }
            productGuiGe.setProductId(productId);
            System.out.println(guigeMap.get("proColor"));
            productGuiGe.setProColor(guigeMap.get("proColor"));

            if (guigeMap.get("proHeight") != null && guigeMap.get("proHeight") != "") {
                proHeight = Double.parseDouble(guigeMap.get("proHeight"));
            }
            productGuiGe.setProHeight(proHeight);
            if (guigeMap.get("proLength") != null && guigeMap.get("proLength") != "") {
                proLength = Double.parseDouble(guigeMap.get("proLength"));
            }
            productGuiGe.setProLength(proLength);

            if (guigeMap.get("proSize") != null && guigeMap.get("proSize") != "") {
                proSize = Double.parseDouble(guigeMap.get("proSize"));
            }
            productGuiGe.setProSize(proSize);
            if (guigeMap.get("proWeight") != null && guigeMap.get("proWeight") != "") {
                proWeight = Double.parseDouble(guigeMap.get("proWeight"));
            }
            productGuiGe.setProWeight(proWeight);
            if (guigeMap.get("proTiji") != null && guigeMap.get("proTiji") != "") {
                proTiji = Double.parseDouble(guigeMap.get("proTiji"));
            }
            productGuiGe.setProTiji(proTiji);

            if (guigeMap.get("proWidth") != null && guigeMap.get("proWidth") != "") {
                proWidth = Double.parseDouble(guigeMap.get("proWidth"));
            }
            productGuiGe.setProWidth(proWidth);

            if (guigeMap.get("proVolume") != null && guigeMap.get("proVolume") != "") {
                proVolume = Double.parseDouble(guigeMap.get("proVolume"));
            }
            productGuiGe.setProVolume(proVolume);

            if (guigeMap.get("buyCount") != null && guigeMap.get("buyCount") != "") {
                buyCount = Integer.parseInt(guigeMap.get("buyCount"));
            }
            productGuiGe.setBuyCount(buyCount);
            if (guigeMap.get("buyCountTotal") != null && guigeMap.get("buyCountTotal") != "") {
                buyCountTotal = Integer.parseInt(guigeMap.get("buyCountTotal"));
            }
            productGuiGe.setBuyCountTotal(buyCountTotal);
            productGuiGe.setBuySign(guigeMap.get("buySign"));
            productGuiGe.setCommissionSign(guigeMap.get("commissionSign"));

            productGuiGe.setImgurl(guigeImgPath);//规格图
            StringBuilder sb = new StringBuilder();


            String guigeGroupPath = "";

            for (int i = 0; i < groupList.size(); i++) {
                sb.append(groupList.get(i) + ",");
            }
            guigeGroupPath = sb.toString();
            productGuiGe.setImgGroup(guigeGroupPath);

            int row = productGuiGeService.insertSelective(productGuiGe);
            int guigeId = productGuiGe.getGuigeId();
            guigeGroupPath = null;
            guigeGroupArr = null;
            System.out.println("------------添加规格图片----->" + guigeGroupArr);
            groupList.clear();//清除所有元素
            System.out.println("------------添加规格图片s----->" + groupList.size());
            //保存价格
            ProductPrice productPrice = new ProductPrice();
            if (guigeMap.get("proPrice") != null && guigeMap.get("proPrice") != "") {
                proPrice = new BigDecimal(guigeMap.get("proPrice"));
            }
            productPrice.setProPrice(proPrice);
            if (guigeMap.get("proMarketPrice") != null && guigeMap.get("proMarketPrice") != "") {
                proMarketPrice = new BigDecimal(guigeMap.get("proMarketPrice"));
            }
            productPrice.setMarketprice(proMarketPrice);

            if (guigeMap.get("proCost") != null && guigeMap.get("proCost") != "") {
                proCost = new BigDecimal(guigeMap.get("proCost"));
            }
            productPrice.setProductCost(proCost);

            if (guigeMap.get("opratorCost") != null && guigeMap.get("opratorCost") != "") {
                opratorCost = new BigDecimal(guigeMap.get("opratorCost"));
            }
            productPrice.setOpratorCost(opratorCost);

            if (guigeMap.get("coupon") != null && guigeMap.get("coupon") != "") {
                coupon = new BigDecimal(guigeMap.get("coupon"));
            }
            productPrice.setCoupon(coupon);

            if (guigeMap.get("visitor") != null && guigeMap.get("visitor") != "") {
                visitor = Integer.parseInt(guigeMap.get("visitor"));
            }
            productPrice.setVisitor(visitor);

            if (guigeMap.get("huiYuan") != null && guigeMap.get("huiYuan") != "") {
                huiYuan = Integer.parseInt(guigeMap.get("huiYuan"));
            }
            productPrice.setHuiyuan(huiYuan);
            if (guigeMap.get("dianZhu") != null && guigeMap.get("dianZhu") != "") {
                dianZhu = Integer.parseInt(guigeMap.get("dianZhu"));
            }
            productPrice.setDianzhu(dianZhu);
            if (guigeMap.get("YZDianZhu") != null && guigeMap.get("YZDianZhu") != "") {
                YZDianZhu = Integer.parseInt(guigeMap.get("YZDianZhu"));
            }
            productPrice.setYzDianzhu(YZDianZhu);
            if (guigeMap.get("otherRank") != null && guigeMap.get("otherRank") != "") {
                otherRank = Integer.parseInt(guigeMap.get("otherRank"));
            }
            productPrice.setOtherRank(otherRank);
            productPrice.setProductId(productId);
            productPrice.setGuigeId(guigeId);
            int rows = productPriceService.insertSelective(productPrice);
            if (row > 0 && rows > 0) {
                map.put("msg", "1");

            }
        }
        return map;

    }


}
